package svc

import (
	"center-data-platform/app/content/model"
	"center-data-platform/app/content/rpc/internal/config"
	"center-data-platform/app/user/rpc/userclient"
	"github.com/zeromicro/go-queue/kq"
	"github.com/zeromicro/go-zero/zrpc"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type ServiceContext struct {
	Config       config.Config
	UserRpc      userclient.User
	ContentModel *model.Content
	DB           *gorm.DB
	KafkaPusher  *kq.Pusher
}

func NewServiceContext(c config.Config) *ServiceContext {
	gormDB, err := gorm.Open(mysql.Open(c.Mysql.Datasource), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic(err)
	}
	return &ServiceContext{
		Config:       c,
		UserRpc:      userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
		ContentModel: &model.Content{},
		DB:           gormDB,
		KafkaPusher:  kq.NewPusher(c.KafkaPusher.Brokers, c.KafkaPusher.Topic),
	}
}
